import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import { formatRichText, timeDifference } from '../../utils/util'
var WSCoordinate = require('../../utils/WSCoordinate.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: getApp().imageBaseURL,
    imageResizeParams: getApp().imageResizeParams,
    content:'',//详情
    pinpailist:[],//品牌活动
    fengcailist:[],//风采活动
    youthid:'',
    gourl:''
  },
  //首页
  navhome(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  //逛逛之家
  navGgzj(e){
    wx.navigateTo({
      url: '/pages/index_youthhome_ggzj/index?id=' + e.currentTarget.dataset.id,
    })
  },
  //反馈
  goFeedback(e){
    if( !api.isauth() ){
      wx.navigateTo({
        url: this.data.gourl
      })
      return
    }
    wx.navigateTo({
      url: '/pages/list_feedback/index?id=' + e.currentTarget.dataset.id,
    })
  },
  //更多品牌
  navPinpaiList(e){
    wx.navigateTo({
      url: '/pages/index_activePinpai/index?youthid=' + e.currentTarget.dataset.youthid,
    })
  },
  //更多最新活动
  navNewList(e){
    wx.navigateTo({
      url: '/pages/index_activeNew/index?youthid=' + e.currentTarget.dataset.youthid,
    })
  },
  goYouthCommunity(e){
    wx.navigateTo({
      url: '/pages/index_youthhome_community/index?id=' + e.currentTarget.dataset.id,
    })
  },
  
  random(min,max){
    if(min > max){
        var ls = min;
        min = max;
        max = ls;
    }
    return Math.floor(Math.random() * (max-min+1) ) + min
  },
  //随机颜色
  randomRgb(){
    return "rgb("+this.random(0,255)+","+this.random(0,255)+","+this.random(0,255)+")";
  },
  
  //打电话
  callphone(){
    let phone = this.data.content.responsibility[0].tel
    console.log(phone)
    wx.makePhoneCall({
      phoneNumber: phone
    }).then(res => console.log('res: ', res))
  },
  openmap(){
    let lat = parseFloat(this.data.content.latitude)
    let lng = parseFloat(this.data.content.longitude)
    let point = WSCoordinate.transformFromBaiduToGCJ(lat, lng);
    console.log(lat,lng)
    wx.openLocation({
      latitude:point.latitude,
      longitude:point.longitude,
      scale: 18
    }).then(res => console.log('res: ', res))
  },

  //详情
  async getDetail (id) {
    let data={}
    data['flag'] = 'activity_community'
    data['communityid'] = id
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result.growEvent.forEach((item,index) => {
        item.color = this.randomRgb()
      })
      console.log(result)
      let imgurl = []
      result.imgurl.forEach((ite,ind) => {
        let url = this.loadImg(ite)
        imgurl.push(url)
      })
      result.imgurl = imgurl
      this.setData({
        content:result,
      })
    }else {
      Toast(message)
    }
  },

  //关注
  async focus (e) {
    if( !api.isauth() ){
      wx.navigateTo({
        url: this.data.gourl
      })
      return
    }
    
    let id = e.currentTarget.dataset.id
    let status = e.currentTarget.dataset.focus
    if(status){
      status = 0;
    }else{
      status = 1
    }

    let data={}
    data['flag'] = 'activity_community_focus'
    data['communityid'] = id
    data['status'] = status
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      this.getDetail(id)
    }else {
      Toast(message)
    }
  },
  //品牌活动
  async getPinpaiActive (id) {
    let data={}
    data['flag'] = 'v2qnzj_info_brandact'
    data['youthid'] = id
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result.forEach((item)=>{
        item.picurl = api.checkurl(item.picurl)
      })
      this.setData({
        pinpailist:result,
      })
    }else {
      // Toast(message)
    }
  },
  loadImg(img){
    let url =  api.checkurl(img)
    return url
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.youthid||''
    if( options.scene ){
      let obj = api.decodeurl(options.scene)
      if( obj ) id = obj.youthid||''
    }
    let gourl = '/pages/login/index?url='+encodeURIComponent('/pages/index_youthhome_detail/index?youthid='+id)
    this.setData({youthid:id,gourl})
    this.getDetail(id)
    this.getPinpaiActive(id)
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户分享自定义
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    api.sendshare(this.data.youthid,4)
    return {
      title: '青年之家详情',
      path: `/pages/index_youthhome_detail/index?youthid=${this.data.youthid}`,
      success(res){
        console.log('转发成功' + res)
      },
      fail(res){
        console.log('转发失败' + res);
      }
    }
  }
})